#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import exceptions
import random

def catchException(exception, value, tb):
    """let Control+C works ;)"""
    if exception == exceptions.KeyboardInterrupt:
        print("\nKeyboard interrupt.. exiting..")
        sys.exit()

def sNum(num):
    return [int(j) for j in str(num)]

def dictSort(adict):
    keys = adict.values()
    keys.sort()
    print keys
    return map(adict.get, keys)

class guessOrDie:
    """Class GuessOrDie, it is main class"""
    def __init__(self):
        self.findPossibleNumbers()
        self.myNumber=self.possibleNumbers[0]
        self.myNumber_=sNum(self.myNumber)
        self.state=1
        self.counter=0

    def findPossibleNumbers(self):
        self.possibleNumbers=[]
        for i in range(1020,9877):
            if self.checkNumber(i):
                self.possibleNumbers.append(i)
        random.shuffle(self.possibleNumbers)

    def checkNumber(self,num):
        return len(set(sNum(num)))==4

    def playerGuess(self):
        if self.state==1:
            num = input("Your Guess : ")
            if self.checkNumber(num)==True and len(sNum(num))==4:
                self.showResultFor(num)
            else:
                print "It's not acceptable value !!"
                self.playerGuess()

    def guessForFirstTime(self):
        random.shuffle(self.possibleNumbers)
        for j in self.possibleNumbers:
            if 0 not in sNum(j):
                return j

    def giveScore(self):
        c={}
        for i in range(10):
            c[i]=0
        for i in self.possibleNumbers:
            for j in sNum(i):
                c[j]+=1
        print c
        return dictSort(c)

    def computerGuess(self):
        if self.state==1:
            if self.counter==0:
                myGuess=self.guessForFirstTime()
            else:
                myGuess=self.possibleNumbers[0]

            print "My guess for your number is : ",myGuess
            result=input("Enter the result in +,- format : ")
            if result[0]==4:
                print 'Yes I did..'
                self.state=0

            self.eliminateByResult(result,myGuess)
            #print len(self.possibleNumbers)
            #self.possibleNumbers.sort()
            #print self.possibleNumbers
            print self.giveScore()
        self.counter+=1

    def eliminateByResult(self,result,guess):
        temp_=[]
        for i in self.possibleNumbers:
            temp=self.getResultFor(i,sNum(guess))
            if (temp[0]==result[0] and temp[1]==result[1]):
                #print i,"is possible !!"
                temp_.append(i)
            self.possibleNumbers=temp_

    def getResultFor(self,num,against):
        correctNum=correctFull=j=0
        for i in sNum(num):
            if i in against:
                if sNum(num)[j]==against[j]:
                    correctFull+=1
                else:
                    correctNum+=1
            j+=1
        return [correctFull,correctNum]

    def showResultFor(self,num):
        if num==self.myNumber:
            print "Yes You did :)"
            self.state=0
        result = self.getResultFor(num,self.myNumber_)
        print "You did : +",result[0],", -",result[1]

if __name__=="__main__":
    #sys.excepthook=catchException
    first=0
    base = guessOrDie()
    print 'My number is : ',base.myNumber
    while first not in [1,2]:
        first = input("Who is first ? [1:Computer, 2:Player] :: ")
    while (base.state):
        if first==2:
            base.playerGuess()
            base.computerGuess()
        else:
            base.computerGuess()
            base.playerGuess()
